package com.hxd.mall.module.product.pictureviewer

import android.content.Context
import android.util.AttributeSet
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.FrameLayout
import android.widget.ImageView
import android.widget.TextView
import androidx.viewpager.widget.PagerAdapter
import androidx.viewpager.widget.ViewPager
import com.hxd.mall.R
import com.hxd.mall.utils.open.PictureUtil.isGif
import com.hxd.mall.utils.open.load
import com.hxd.mall.utils.open.loadGif
import com.shizhefei.view.largeimage.LargeImageView
import java.util.*

class LargePictureView constructor(val mContext: Context, attrs: AttributeSet, defStyleAttr: Int) : FrameLayout(mContext, attrs, defStyleAttr),
    ViewPager.OnPageChangeListener {
    private var mImageUrls: List<String>? = null
    private val mImageViews: MutableList<View>
    private var mCount: Int = 0
    private var mCurrentIndex = 1

    //    private var mPrevious: ImageView? = null
//    private var mNext: ImageView? = null
    private var mIndicator: TextView? = null
    private var mViewPager: ViewPager? = null
    private var mAdapter: PictureViewPagerAdapter? = null
    private var mLisenter: LargePictureView.OnClickeListener? = null

    init {

        mImageUrls = ArrayList()
        mImageViews = ArrayList()
        initView()
    }

    private fun initView() {
        val view = LayoutInflater.from(mContext).inflate(R.layout.picture_view_layout, this, true)
//        mPrevious = view.findViewById(R.id.previous)
//        mPrevious!!.visibility = View.GONE
//        mNext = view.findViewById(R.id.next)
//        mNext!!.visibility = View.GONE
        mIndicator = view.findViewById(R.id.indicator)
        mViewPager = view.findViewById(R.id.view_pager)

//        mPrevious!!.setOnClickListener {
//            mCurrentIndex = (mCurrentIndex - 1) % mCount
//            mViewPager!!.currentItem = mCurrentIndex
//        }
//
//        mNext!!.setOnClickListener {
//            mCurrentIndex = (mCurrentIndex + 1) % mCount
//            mViewPager!!.currentItem = mCurrentIndex
//        }
    }

    fun setImages(imageUrls: List<String>): LargePictureView {
        mImageUrls = imageUrls
        mCount = imageUrls.size
        return this
    }

    fun getItem(index: Int): String {
        return mImageUrls!![index]
    }

    fun show() {
        setImageList(mImageUrls)
        setData()
    }

    private fun setData() {
        if (mAdapter == null) {
            mAdapter = PictureViewPagerAdapter()
        }
        mViewPager!!.adapter = mAdapter
        mViewPager!!.addOnPageChangeListener(this)
        mViewPager!!.currentItem = 0
    }

    fun setPage(page: Int) {
        mViewPager!!.currentItem = page
    }

    private fun setImageList(imagesUrl: List<String>?) {
        if (imagesUrl == null || imagesUrl.size <= 0) {
            return
        }
        initImages()
        for (i in imagesUrl.indices) {
            var largeImageView: View? = null
            if (largeImageView == null) {

                if (isGif(imagesUrl[i])) {
                    largeImageView = ImageView(mContext)
                    largeImageView.loadGif(imagesUrl[i])
                } else {
                    largeImageView = LargeImageView(mContext)
                    largeImageView.setCriticalScaleValueHook(object :
                        LargeImageView.CriticalScaleValueHook {
                        override fun getMinScale(
                            largeImageView: LargeImageView,
                            imageWidth: Int,
                            imageHeight: Int,
                            suggestMinScale: Float
                        ): Float {
                            return 1f
                        }

                        override fun getMaxScale(
                            largeImageView: LargeImageView,
                            imageWidth: Int,
                            imageHeight: Int,
                            suggestMaxScale: Float
                        ): Float {
                            return 4f
                        }
                    })
                    largeImageView.load(imagesUrl[i], R.drawable.banner_default_icon)
                }
                largeImageView.setOnClickListener {
                    if (mLisenter != null) {
                        mLisenter!!.onClick(i)
                    }
                }

                largeImageView.setOnLongClickListener {
                    if (mLisenter != null) {
                        mLisenter!!.onLongClick(this@LargePictureView, i)
                    }
                    false
                }
            }
            mImageViews.add(largeImageView)
        }

//        if (mImageViews.size <= 1) {
//            mPrevious!!.visibility = View.GONE
//            mNext!!.visibility = View.GONE
//        } else {
//            mNext!!.visibility = View.VISIBLE
//        }
    }

    private fun initImages() {
        mImageViews.clear()
        mIndicator!!.text = "$mCurrentIndex/$mCount"
    }

    override fun onPageScrolled(i: Int, v: Float, i1: Int) {

    }

    override fun onPageSelected(i: Int) {
//        if (i == 0) {
//            mPrevious!!.visibility = View.GONE
//            mNext!!.visibility = View.VISIBLE
//        } else if (i == mImageViews.size - 1) {
//            mPrevious!!.visibility = View.VISIBLE
//            mNext!!.visibility = View.GONE
//        } else {
//            mPrevious!!.visibility = View.VISIBLE
//            mNext!!.visibility = View.VISIBLE
//        }
        mIndicator!!.text = (i + 1).toString() + "/" + mCount
    }

    override fun onPageScrollStateChanged(i: Int) {

    }

    fun setLisenter(lisenter: LargePictureView.OnClickeListener) {
        this.mLisenter = lisenter
    }

    internal inner class PictureViewPagerAdapter : PagerAdapter() {

        override fun getCount(): Int {
            return mImageViews.size
        }

        override fun isViewFromObject(view: View, `object`: Any): Boolean {
            return view === `object`
        }

        override fun instantiateItem(container: ViewGroup, position: Int): Any {
            container.addView(mImageViews[position])
            return mImageViews[position]
        }

        override fun destroyItem(container: ViewGroup, position: Int, `object`: Any) {
            container.removeView(`object` as View)
        }
    }

    interface OnClickeListener {
        fun onClick(index: Int)

        fun onLongClick(v: View, index: Int)
    }
}
